<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2021/7/5
 * Time: 6:10 下午
 */

namespace app\utils;


class RsaUtils
{
    protected static $publicKey = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2djvgBWr8CliI2fbEnTN
bB8MUOrX6hKyXKOChiZzs7lyaAqMPsy0DSV5XFPbP1jlp9zLiTepOZ1ZKDQHw6TZ
mIeBBIDJ0TbiHCErpLv+P2zJuPWDJrkPQwf1WLZgdEO4bel3udhLq1XM55bqkO/m
F8RvbCmNrxSpzwns+3Y2OgMYZp5/MWbQlcHcXyWXz7TMpsXNyRIS5x2VovzEvjJO
3QU3sf7uDFys74Yz4vrt/EWS0SULBAX9D7Gq96+58t0Tu6Ef8AY8r1dlviiTr87j
nuZ4qoMDGVDSib0ehX12C7doK0RHaYYIvhIV1VT0NFFqPNT91ikLRNROUUYzPK1G
WwIDAQAB
-----END PUBLIC KEY-----';
    public static $privateKey = '-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZ2O+AFavwKWIj
Z9sSdM1sHwxQ6tfqErJco4KGJnOzuXJoCow+zLQNJXlcU9s/WOWn3MuJN6k5nVko
NAfDpNmYh4EEgMnRNuIcISuku/4/bMm49YMmuQ9DB/VYtmB0Q7ht6Xe52EurVczn
luqQ7+YXxG9sKY2vFKnPCez7djY6Axhmnn8xZtCVwdxfJZfPtMymxc3JEhLnHZWi
/MS+Mk7dBTex/u4MXKzvhjPi+u38RZLRJQsEBf0Psar3r7ny3RO7oR/wBjyvV2W+
KJOvzuOe5niqgwMZUNKJvR6FfXYLt2grREdphgi+EhXVVPQ0UWo81P3WKQtE1E5R
RjM8rUZbAgMBAAECggEAW3X735+5p0QIourIjDAjysAiUJq4edrNrZ0sgJwGOa+j
O9Ep3nfdJCPWlw/R3f+5s8c8YOjQkJLiSsHFfLtqa+KAJV7Y3JCZT8w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czsWTMoA9T4qeUylp9AUH/nWjjC2hqO99SnagXO+
t+MpEL3wQHOlPf6zaDzSe6E=
-----END PRIVATE KEY-----';
    public static function encrypt ($str): string {
        openssl_public_encrypt($str, $encrypted, self::$publicKey);
        return base64_encode($encrypted);
    }

    public static function decrypt ($encrypted) {
        $encrypted = base64_decode($encrypted);
        openssl_private_decrypt($encrypted, $decrypted, self::$privateKey);
        return $decrypted;
    }
}
